package com.ticket.sass.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.CouponUserMapMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.CouponUserMapEntity;
import com.ticket.sass.common.entity.tenant.enums.CouponUserMapUseState;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券领取表 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-13
 */
@Service
public class CouponUserMapServiceImpl extends ServiceImpl<CouponUserMapMapper, CouponUserMapEntity> implements BaseService<CouponUserMapEntity> {
    
    public Map<String, CouponUserMapUseState> getCouponUseStateByCouponId(Long couponId, List<String> code) {
        if (code.size() == 0){
            return new HashMap<>();
        }
        // 创建查询条件
        QueryWrapper<CouponUserMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
            .eq(CouponUserMapEntity::getCouponId, couponId)
            .in(CouponUserMapEntity::getCode, code);
        
        // 执行查询
        List<CouponUserMapEntity> couponUserMapList = this.baseMapper.selectList(queryWrapper);
        
        // 将查询结果转换为Map<code, use_state>
        return couponUserMapList.stream()
            .collect(Collectors.toMap(
                CouponUserMapEntity::getCode,
                CouponUserMapEntity::getUseState
            ));
    }
    
    public List<String> codeBy(Long couponId, Short useState) {
        if (Objects.isNull(useState)){
            return new ArrayList<>();
        }
        
        // 创建查询条件
        QueryWrapper<CouponUserMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
            .eq(CouponUserMapEntity::getCouponId, couponId)
            .eq(CouponUserMapEntity::getUseState, useState);
        
        // 执行查询
        List<CouponUserMapEntity> couponUserMapList = this.baseMapper.selectList(queryWrapper);
        
        // 从查询结果中提取code字段并返回列表
        return couponUserMapList.stream()
            .map(CouponUserMapEntity::getCode)
            .collect(Collectors.toList());
    }
}
